<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" icon="el-icon-delete" class="handle-del mr10"
          >批量删除</el-button
        >
        <!-- <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
                    <el-option key="1" label="广东省" value="广东省"></el-option>
                    <el-option key="2" label="湖南省" value="湖南省"></el-option>
                </el-select> -->
        <el-input
          v-model="query.username"
          placeholder="用户名"
          class="handle-input mr10"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="searchDataByName">搜索</el-button>
      </div>
      <el-table
        :data="enterpriseData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="user_id"
          label="ID"
          width="80"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="用户账号"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="enter_name"
          label="企业名称"
          width="300"
        ></el-table-column>
        <el-table-column
          prop="enter_phone"
          label="企业电话"
          width="160"
        ></el-table-column>
        <el-table-column prop="user_state" label="状态" width="80">
          <template slot-scope="scope">
            <el-tag>{{ getUserState(scope.row.user_state) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="apply_time"
          width="200"
          label="申请时间"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="applyEdit(scope.$index, scope.row)"
              >审核</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.currentPage"
          :page-size="query.pageSize"
          :page-count="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog
      title="企业开户审核"
      :visible.sync="checkVisible"
      width="30%"
      :append-to-body="true"
    >
      <el-form ref="form" :model="form" label-width="70px">
        <div class="hidden">
          {{form.user_id}}
        </div>
        <el-form-item label="申请用户">
          <label>{{ form.username }}</label>
        </el-form-item>

        <el-form-item label="开户状态">
          <label
            ><el-tag>{{ getUserState(form.user_state) }}</el-tag></label
          >
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" @click="checkOK()">审核通过</el-button>
        <el-button type="danger">审核驳回</el-button>
        <el-button @click="checkVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getApplyingEnterpriseDataMapper, CheckAccount } from "@/api";
export default {
  name: "CheckEnterpriseView",
  data() {
    return {
      //查询搜索提交的表单
      query: {
        username: "",
        currentPage: 1,
        pageSize: 10,
      },
      enterpriseData: [],
      form: {
      },
      pageTotal: 0, //分页
      checkVisible: false, //默认审核框不可见
      addVisible: false,
    };
  },
  mounted() {
    //加载页面后初始化数据
    this.initData();
  },
  methods: {
    initData() {
      this.getApplyingEnterpriseDataMapper();
    },
    async getApplyingEnterpriseDataMapper() {
      let res = await getApplyingEnterpriseDataMapper(this.query);
      console.log(res);
      if (res.code == 200) {
        this.enterpriseData = res.data.data;
        this.pageTotal = res.data.pageTotal;
      }
    },
    //搜索
    searchDataByName(){
      this.getApplyingEnterpriseDataMapper();
      this.query.username = "";
    },
    checkAccount(){
      console.log(this.form.user_id);
    },
    //审核通过操作方法
    async checkOK() {
      this.form.v_user_id = JSON.parse(localStorage.getItem("User")).user_id; //用户角色验证
      // console.log(this.form);
      let res = await CheckAccount(this.form);
      if (res.code == 200) {
        this.$message({
          message: res.msg,
          type: "success",
        });
        //关闭审核窗口
        this.checkVisible = false;
        //重新加载页面
        this.initData();
      } else {
        this.$message.error(res.msg);
      }
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页导航
    handlePageChange(val) {
      // console.log(val);
      // this.$set(this.query, 'currentPage', val);
      this.query.currentPage = val;
      this.initData();
    },
    //审核按钮
    applyEdit(index, row) {
      this.form = {};
      this.idx = index;
      this.form = row;
      this.checkVisible = true;
    },
    //选择状态
    getUserState(num) {
      switch (num) {
        case 0:
          return "未开户";
        case 1:
          return "已开户";
        case 2:
          return "申请中";
        case 3:
          return "驳回";
        case 6:
          return "禁用";
        default:
          return "默认未开户";
      }
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.mg-left {
  margin-left: 10px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}

.hidden{
  display: none;
}
</style>